// map.js
import qqMap from '../../../utils/qqmap'
Page({
  onLoad({
    id
  }) {
    this.getRepairDetail(id)
    // 调用地图
    this.createPolyLine()
    this.setData({
      repairId: id
    })
  },
  data: {
    repairId: '',
    latitude: 40.060539,
    longitude: 116.343847,
    statusMsg: {
      0: '已取消',
      1: '受理中',
      2: '上门中',
      3: '已完成',
    },
    statusType: {
      0: 'cancel',
      1: 'info',
      2: 'success',
      3: 'complete',
    },
    dialogVisible: false,
    markers: [{
        id: 1,
        latitude: 40.086757,
        longitude: 116.328634,
        width: 24,
        height: 30,
      },
      {
        id: 2,
        latitude: 40.08346500000002,
        longitude: 116.33293800000011,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40,
      },
    ],
  },
  async getRepairDetail(id) {
    const {
      data
    } = await wx.http.get(`/repair/${id}`)
    console.log(data);
    this.setData({
      ...data
    })
  },
  openDialog() {
    this.setData({
      dialogVisible: true
    })
  },
  async onComfirm() {
    // console.log('111');
    await wx.http.put(`/cancel/repaire/${this.data.repairId}`)
    wx.utils.toast('取消成功')
    wx.navigateBack()
  },
  Modify() {
    wx.navigateTo({
      url: '/repair_pkg/pages/form/index?id=' + this.data.repairId,
    })
  },
  //报修上门路线规划解压
  createPolyLine() {
    //调用距离计算接口
    qqMap.direction({
      mode: 'bicycling',
      //from参数不填默认当前地址
      from: '40.060539,116.343847', // 传智播客
      to: '40.086757,116.328634', // （示例）云趣园1区
      success: (res) => {
        let ret = res;
        let coors = ret.result.routes[0].polyline,
          pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        let kr = 1000000;
        for (let i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (let i = 0; i < coors.length; i += 2) {
          pl.push({
            latitude: coors[i],
            longitude: coors[i + 1]
          })
        }
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        this.setData({
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4,
          }, ],
        })
      },
      fail: function (error) {
        console.error(error)
      },
    })
  },
})